package com.bytecodezz.app.toolset.launcher

import android.content.Context
import android.os.Build
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.WindowManager
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import com.bytecodezz.app.toolset.R
import com.bytecodezz.app.toolset.base.BaseUIActivity
import com.bytecodezz.app.toolset.databinding.ActivityLauncherUiBinding
import com.bytecodezz.app.toolset.idcard.IDcardFragment
import com.bytecodezz.app.toolset.launcher.adapter.ToolMenuAdapter
import com.bytecodezz.app.toolset.launcher.model.LauncherViewModel

class LauncherUI : BaseUIActivity<ActivityLauncherUiBinding, LauncherViewModel>() {
    var menuAdapter: ToolMenuAdapter = ToolMenuAdapter()

    override fun onCreateViewModelClass() = LauncherViewModel::class.java

    override fun onCreateViewBinding() = ActivityLauncherUiBinding.inflate(layoutInflater)

    override fun initOtherObserver() {
        viewModel.closeFragmentEvent.observe(this) {
            hideFunContentPager(it)
        }
        viewModel.toolMenusEvent.observe(this) {
            menuAdapter.refreshData(it)
        }
    }

    override fun onActionStart(savedInstanceState: Bundle?) {
        initView()
        initRecycler()
        viewModel.createListData()
    }

    private fun initView() {
        binding?.text?.setOnClickListener {
            showToast("点击统计信息")
        }
    }

    private fun initRecycler() {
        menuAdapter.setOnItemClickListener { _, position ->
            when (menuAdapter.getItemData(position).id) {
                1L -> {
                    showFunContentPager(
                        supportFragmentManager.findFragmentByTag(IDcardFragment.TAG)
                            ?: IDcardFragment(),
                        IDcardFragment.TAG
                    )
                }
            }
        }
        val screenHeight: Int = getScreenHeight(this)
        binding!!.toolsMenuRecycler.run {
            layoutManager = GridLayoutManager(this@LauncherUI, 2)
            layoutParams.height = screenHeight / 2
            adapter = menuAdapter

        }
    }

    private fun showFunContentPager(fragment: Fragment, tag: String) {
        //显示身份证识别主页
        with(supportFragmentManager.beginTransaction()) {
            if (fragment.isAdded) {
                show(fragment)
            } else {
                add(R.id.tools_content_pager, fragment, tag)
            }
            commit()
        }
    }

    private fun hideFunContentPager(tag: String) {
        //显示身份证识别主页
        with(supportFragmentManager.beginTransaction()) {
            supportFragmentManager.findFragmentByTag(tag)?.let {
                hide(it)
            }
            commit()
        }
    }

    private fun getScreenHeight(context: Context): Int {
        var dm: DisplayMetrics? = null
        try {
            dm = DisplayMetrics()
            val localWindowManager = context.getSystemService(WINDOW_SERVICE) as WindowManager
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                localWindowManager.defaultDisplay.getRealMetrics(dm)
            } else {
                localWindowManager.defaultDisplay.getMetrics(dm)
            }
            return dm.heightPixels
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return 0
    }
}